package sdk.login.tvos.com.danmutest02;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.VideoView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master.flame.danmaku.ui.widget.DanmakuView;

public class DanmuActivity extends Activity implements View.OnClickListener {

    @InjectView(R.id.sv_danmaku)
    DanmakuView mDanmakuView;
    @InjectView(R.id.btn_start)
    Button btnStart;
    @InjectView(R.id.editText)
    EditText editText;
    @InjectView(R.id.btn_send)
    Button mBtnSendDanmaku;
    @InjectView(R.id.btn_pause)
    Button btnPause;
    @InjectView(R.id.btn_resume)
    Button btnResume;
    @InjectView(R.id.btn_hide)
    Button btnHide;
    @InjectView(R.id.btn_show)
    Button btnShow;
    @InjectView(R.id.btn_loop)
    Button btnLoop;
    @InjectView(R.id.btn_location)
    Button btnLocation;
    @InjectView(R.id.btn_stop)
    Button btnStop;

    private DanmuControl danmuControl;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_danmuku);
        ButterKnife.inject(this);
        VideoView mVideoView = (VideoView) findViewById(R.id.videoview);

        //弹幕控制类
        danmuControl = new DanmuControl(this, mDanmakuView);


        if (mVideoView != null) {
            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
            mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/ha.avi");
        }
    }


    @OnClick({R.id.btn_start, R.id.btn_send, R.id.btn_pause, R.id.btn_resume, R.id.btn_hide, R.id.btn_show, R.id.btn_loop, R.id.btn_location, R.id.btn_stop})
    public void onClick(View view) {
        final String imageUrl = "http://fxcdn9.audiocn.com/upload_files/images/360_270/20160826/df3cfdbf-0a1d-4e98-861f-ddf8a3235f61.png";

        switch (view.getId()) {
            case R.id.btn_start:
                danmuControl.startDanMu();
                break;
            case R.id.btn_send:
                if (!TextUtils.isEmpty(editText.getText())) {
                    danmuControl.addDanmu(imageUrl,"wall-err", editText.getText().toString());
                }
                break;
            case R.id.btn_pause:
                danmuControl.pauseDanMu();
                break;
            case R.id.btn_resume:
                danmuControl.resumeDanMu();
                break;
            case R.id.btn_hide:
                danmuControl.hideDanMu();
                break;
            case R.id.btn_show:
                danmuControl.showDanMu();
                break;
            case R.id.btn_loop:
                final String content = "三星NOTE7又爆炸了,我要买华为";
                danmuControl.startDanMu();
                handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        if (msg.what == 0) {
                            danmuControl.addDanmu(imageUrl, "wall-ee",content);
                            handler.sendEmptyMessageDelayed(0, 500);
                        }
                    }
                };
                handler.sendEmptyMessageDelayed(0, 500);
                break;
            case R.id.btn_location:
                danmuControl.parserLocation(getResources().openRawResource(R.raw.comments));
                danmuControl.startDanMu();
                break;
            case R.id.btn_stop:
                danmuControl.stopDanMu();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeMessages(0);
        }
        mDanmakuView = null;
        danmuControl = null;
        super.onDestroy();
    }
}
